//package mp3seller.servlets;
//
//import java.io.File;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.Iterator;
//import java.util.List;
//
//import javax.servlet.ServletConfig;
//import javax.servlet.ServletContext;
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.FileUploadException;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//
//
//
///**
// * Servlet e de controle de upload
// * 
// * @author Raoni
// * 
// */
//@WebServlet("/ServletUpload")
//public class ServletUpload extends HttpServlet {
//
//	private static final String TEMP_PATH = "/filesMp3/";
//	private File tmpDir;
//	private static final String DESTINATION_DIR_PATH = "/filesMp3/";
//	private File destinationDir;
//
//	/**
//	 * inicializa as configurações de upload de imagens
//	 */
//	public void init(ServletConfig config) throws ServletException {
//		super.init(config);
//		ServletContext sc = getServletContext();
//		tmpDir = new File(sc.getRealPath(TEMP_PATH));
//		if (!tmpDir.isDirectory()) {
//			throw new ServletException(TEMP_PATH + " is not a directory");
//		}
//		String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
//		destinationDir = new File(realPath);
//		if (!destinationDir.isDirectory()) {
//			throw new ServletException(DESTINATION_DIR_PATH
//					+ " is not a directory");
//		}
//
//	}
//
//	/**
//	 * metodo especial que faz upload do arquivo solicitado
//	 */
//	protected void doPost(HttpServletRequest request,
//			HttpServletResponse response) throws ServletException, IOException {		
//
//		PrintWriter out = response.getWriter();
//		response.setContentType("text/html");
//
//		DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
//		fileItemFactory.setSizeThreshold(1 * 1024 * 1024); // 1 MB
//		fileItemFactory.setRepository(tmpDir);
//
//		ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
//		try {
//			List items = uploadHandler.parseRequest(request);
//			Iterator itr = items.iterator();
//			while (itr.hasNext()) {
//				FileItem item = (FileItem) itr.next();
//				if (item.isFormField()) {
//					out.println("Nome do campo = " + item.getFieldName()
//							+ ", Value = " + item.getString());
//				} else {
//					out.println("Arquivo = " + item.getName() + "<br/>"
//							+ "Mime Type = " + item.getContentType() + "<br/>"
//							+ "Tamanho= " + item.getSize() / 1024 + " KB");
//					File file = new File(destinationDir,
//							motorista.getMatricula() + "");
//					item.write(file);
//					out.println("<br/>Arquivo gravado em: " + file.getPath());
//				}
//				out.close();
//				// motorista.setFotoCarteira(Hibernate.createBlob(item.getInputStream()));
//				// Session session =
//				// HibernateUtil.getSessionFactory().getCurrentSession();
//				// session.beginTransaction();
//				// session.save(motorista);
//				// Dao.save(motorista);
//
//			}
//		} catch (FileUploadException ex) {
//			ex.printStackTrace();
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//	}
//}
